﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.Common;

namespace Manager.FormAdd_Edit
{
    public partial class frm_Currency : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        TienTe tienTe = null;

        public frm_Currency()
        {
            InitializeComponent();
        }        

        public frm_Currency(TienTe _tienTe)
        {
            InitializeComponent();
            tienTe = _tienTe;
        }

        private void butLuu_Thoat_Click(object sender, EventArgs e)
        {
            this.butLuu_Click(sender, e);
            this.Close();            
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (tienTe != null)
                {
                    var temp = db.TienTes.Single(i => i.MaDonVi == tienTe.MaDonVi);
                    temp.MaDonVi = txtMaTienTe.Text;
                    temp.TenDonVi = txtTenDonVi.Text;
                    temp.TyGiaThamKhao = CommonFunction.ParseDouble(txtTyGia.Text);
                    temp.GhiChu = memoGhiChu.Text;
                }
                else
                {
                    TienTe tt = new TienTe();
                    tt.MaDonVi = txtMaTienTe.Text;
                    tt.TenDonVi = txtTenDonVi.Text;
                    tt.TyGiaThamKhao = CommonFunction.ParseDouble(txtTyGia.Text);
                    tt.GhiChu = memoGhiChu.Text;
                    db.TienTes.InsertOnSubmit(tt);
                }
                db.SubmitChanges();
                MessageBox.Show("Lưu thành công.");
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_Currency:butLuu_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn đóng không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
                this.Close();
        }

        private void frm_Currency_Load(object sender, EventArgs e)
        {
            if (tienTe != null)
            {
                txtMaTienTe.Text = tienTe.MaDonVi;
                txtTenDonVi.Text = tienTe.TenDonVi;
                txtTyGia.Text = CommonFunction.AddSeparator(tienTe.TyGiaThamKhao.Value);
                memoGhiChu.Text = tienTe.GhiChu;
                txtMaTienTe.Enabled = false;
            }
        }

        private void txtTyGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Chỉ được nhập số!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}